//
//  CommentForm.m
//  nova
//
//  Created by user on 11/07/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CommentForm.h"


@implementation CommentForm
@synthesize comment;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self initObjects];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

-(IBAction) openHistory{

}

-(void) initObjects{
	[lblDate setText: [Library getLocalizedDateString: [comment getDate]]];
	[lblOwner setText:[comment getOwner]];
	[txtDescription setText:[comment getDescription]];
}

-(IBAction) textFieldReturn : (id) sender{
	[txtDescription resignFirstResponder];
}

-(IBAction) saveAction{
	[comment setChanged:YES];
	UIAlertView *exitingWithSave = [[UIAlertView alloc] initWithTitle:@"Comment changed. Save it?" message:@"Do you wish to save the changes made to this comment?" delegate:self cancelButtonTitle:@"No"  otherButtonTitles:nil];
	[exitingWithSave addButtonWithTitle:@"Yes"];
	[exitingWithSave show];
}

-(void) alertView : (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	if(buttonIndex == 1)
	{
		DataContext *context = [[DataContext alloc] init];
		if([comment isNew] && [comment changed]){
			@try {
				[comment setChanged:YES];
				[comment setDescription: [txtDescription text]];
				[context executeQuery:[comment getInsertString]];
				[[myAppDelegate getProject] insertNewComment:comment];
			}
			@catch (NSException * e) {
				UIAlertView *exitingWithSave = [[UIAlertView alloc] initWithTitle:@"Error saving to database." message:@"There was an error saving this action. Please restart the aplication!" delegate:nil cancelButtonTitle:@"Ok"  otherButtonTitles:nil];
				[exitingWithSave show];
			}
		}else if([comment changed]){
			[comment setChanged:YES];
			[comment setDescription: [txtDescription text]];
			[context executeQuery:[comment getUpdateString]];
		}
		[context dealloc];
		context = nil;		
	}
}


-(IBAction) cancelChanges{
	[self.view removeFromSuperview];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
